package com.swt.gap.controller;

import com.swt.gap.common.graph.Graph;
import com.swt.gap.model.vo.external.ExternalOntology;
import com.swt.gap.model.vo.external.SearchExternalNodeVO;
import com.swt.gap.model.vo.graph.NodeProperty;
import com.swt.gap.service.ExternalGraphService;
import io.swagger.annotations.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 请填写说明
 *
 * @author sx
 * @date 2020/8/12 14:10
 */
@Api(tags = "外部图谱")
@RestController
@RequestMapping("/external-graph")
public class ExternalGraphController {

    private final ExternalGraphService externalGraphService;

    public ExternalGraphController(ExternalGraphService externalGraphService) {
        this.externalGraphService = externalGraphService;
    }

    @ApiOperation(value = "根据名称搜索外部数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "name",  value = "节点显示名称", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "type",  value = "1:知识库. 2:qb库", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "ooid",
                    value = "类别id,非必填，当填写时只搜索指定的类别"),
    })
    @ApiResponse(code = 200, message = "ok", response = Graph.Node.class, responseContainer = "List")
    @GetMapping("/search-node")
    public ResponseEntity<List<Graph.Node>> searchExternalByNodes(@RequestParam("name") String name,
                                                         @RequestParam("type") Integer type,
                                                         @RequestParam(value = "ooid", required = false) String ooid) {
        return ResponseEntity.ok(externalGraphService.searchExternalByNode(name, type, ooid));
    }

    @ApiOperation(value = "选择匹配的节点，扩展出的数据跟自有数据关联")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "nodeId",  value = "原节点id"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "id",  value = "匹配的外部节点id"),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "type",  value = "1:知识库. 2:qb库")
    })
    @ApiResponse(code = 200, message = "ok", response = Graph.class)
    @GetMapping("/relation-node")
    public ResponseEntity<Graph> linkExternalData(@RequestParam("nodeId") String nodeId,
                                                  @RequestParam("id") String id,
                                                  @RequestParam("type") Integer type) {
        return ResponseEntity.ok(externalGraphService.linkExternalData(nodeId, id, type));
    }

    @ApiOperation(value = "外部节点继续扩展")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "id",  value = "外部节点id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "type",  value = "1:知识库. 2:qb库", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "mergedExternalId",
                    value = "被合并的外部节点id, 非必须，不需要跟自有图谱合并时不填写"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "mergedId",
                    value = "合并后的内部节点id, 非必须，不需要跟自有图谱合并时不填写"),
    })
    @ApiResponse(code = 200, message = "ok", response = Graph.class)
    @GetMapping("/expand-graph")
    public ResponseEntity<Graph> expandExternalData(@RequestParam(value = "id") String id,
                                                    @RequestParam("type") Integer type,
                                                    @RequestParam(value = "mergedExternalId", required = false) String mergedExternalId,
                                                    @RequestParam(value = "mergedId", required = false) String mergedId) {
        return ResponseEntity.ok(externalGraphService.expandExternalData(id, type, mergedExternalId, mergedId));
    }

    @ApiOperation(value = "根据外部节点id，获取节点属性信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "id",  value = "外部节点id"),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "type",  value = "1:知识库. 2:qb库")
    })
    @ApiResponse(code = 200, message = "ok", response = NodeProperty.class, responseContainer = "List")
    @GetMapping("/node-property")
    public ResponseEntity<List<NodeProperty>> getNodeById(@RequestParam("id") String id, @RequestParam("type") Integer type) {
        return ResponseEntity.ok(externalGraphService.getNodeProperty(id, type));
    }

    @ApiOperation(value = "本体树，即对象类型")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "type",  value = "1:知识库. 2:qb库")
    })
    @ApiResponse(code = 200, message = "ok", response = ExternalOntology.class)
    @GetMapping("/ontology")
    public ResponseEntity<ExternalOntology> getNodeById(@RequestParam Integer type) {
        return ResponseEntity.ok(externalGraphService.getExternalOntology(type));
    }

}
